package com.zlk.algorithm.huawei.nowcoder.link;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @program: algorithm
 * @ClassName HJ48
 * @description:链表
 *
 * @author: slfang
 * @create: 2024-12-27 08:37
 * @Version 1.0
 **/
public class HJ48 {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
            String s = in.nextLine();
            String[] sArr = s.split(" ");
            String headVal = sArr[1];
            HashMap<String,Node> nodeMap = new HashMap<>();
            Node[] nodes = new Node[10001];
            Node head = new Node(headVal);
            nodes[Integer.valueOf(headVal)]=head;
            for (int i = 3; i <sArr.length-1 ; i+=2) {
                String nextStr = sArr[i-1];
                String cur = sArr[i];
                Node next = new Node(nextStr);
                nodes[Integer.valueOf(nextStr)]=next;
                if(cur==headVal){
                    next.next = head.next;
                    head.next = next;
                }else{
                    Node curNode = nodes[Integer.valueOf(cur)];
                    if(curNode==null){
                        nodes[Integer.valueOf(cur)] = new Node(cur);
                        curNode = nodes[Integer.valueOf(cur)];
                    }
                    next.next = curNode.next;
                    curNode.next = next;
                }
            }
            String deleteVal = sArr[sArr.length-1];
            Node cur = head;
            if(deleteVal.equals(headVal)){
                printNode(head.next);
            }else{
                Node pre = head;
                while (cur!=null){
                    if(deleteVal.equals(cur.val)){
                        pre.next = cur.next;
                    }else{
                        System.out.print(cur.val);
                        System.out.print(" ");
                    }
                    pre = cur;
                    cur=cur.next;
                }
            }
        }
    }

    private static void printNode(Node cur) {
        while (cur!=null){
            System.out.print(cur.val);
            System.out.print(" ");
            cur = cur.next;
        }
    }

    static class Node{
        String val;
        Node next;

        public Node(String val, Node next) {
            this.val = val;
            this.next = next;
        }

        public Node(String val) {
            this.val = val;
        }
    }

}
